package cate.game.play.skill.effect;

import cate.common.table.d.GD;
import cate.common.table.d.GDSkill;
import cate.common.util.XT;
import cate.game.play.buff.Buff;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.effect.base.SE;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.List;

@NoteClass("驱散")
public class 驱散SE extends SE {

	@NoteField("概率")
	private int chance;

	@NoteField("是否有利")
	private Boolean good;

	@NoteField("是否控制")
	private Boolean isCtrl;

	@NoteField("限制数量")
	private int dispelLimit;

	@NoteField("添加buff基准")
	private byte addBuffBase;

	@NoteField("添加buff")
	private int addBuff;

	@NoteField("添加buff限制数量")
	private int addBuffLimit;

	//概率=10000&是否有利=0&是否控制=0&限制数量=10&添加buff基准=1&添加buff=？？添加buff限制数量=-1
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		chance = args.getInt("概率", GD.W_10000);
		good = args.getBoolean("是否有利",null);
		isCtrl = args.getBoolean("是否控制",null);
		dispelLimit = args.getInt("限制数量", -1);
		addBuffBase = args.getByte("添加buff基准", GDSkill.Base.TARGET);
		addBuff = args.getInt("添加buff", -1);
		addBuffLimit = args.getInt("添加buff限制数量", -1);
	}

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		chance += args.getInt("概率", 0);
	}

	@Override
	public void settle(SkillActionCtx action, Fighter target) {
		if (chance <= 0) {
			return;
		}
		if (!XT.chance(chance)) {
			return;
		}
		addDispelLimit(this.parent.getDispelLimit());
		List<Buff> dispelBuffs = target.buff.tryDispel(action, buff -> {
			if (good != null && good != buff.cfg.row.good) {
				return false;
			}
			if (isCtrl != null && isCtrl != buff.isCtrl()) {
				return false;
			}
			return true;
		}, dispelLimit);

		int dispelNum = dispelBuffs.size();
		if (dispelNum > 0) {
			if (addBuff > 0) {
				int addBuffNum;
				if (addBuffLimit > 0) {
					addBuffNum = Math.min(addBuffLimit, dispelNum);
				} else {
					addBuffNum = dispelNum;
				}
				Fighter addBuffTarget = null;
				switch (addBuffBase) {
					case GDSkill.Base.CASTER:
						addBuffTarget = action.getActor();
						break;
					case GDSkill.Base.TARGET:
						addBuffTarget = target;
						break;
					default:
						break;
				}
				if (addBuffTarget != null) {
					addBuffTarget.buff.tryAddSome(action, action.getActor(), addBuff, addBuffNum);
				}
			}
			//FighterPerformance.recDispel(action, action.getActor(), target, dispelNum);
		}
	}

	public void addDispelLimit(int limit) {
		if (dispelLimit < 0) {
			return;
		}
		if (limit < 0) {
			dispelLimit = -1;
			return;
		}
		dispelLimit += limit;
	}
}
